271B - Prime Matrix - CodeForces Solution


binary search brute force math number theory *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define el '\n'
#define dd long double
#define ff float
#define so sort(a.begin() , a.end())
#define so1 sort(a.rbegin() , a.rend())
#define pb push_back
#define be v.begin()
#define be1 v.rbegin()
#define en v.end()
#define en1 v.rend()
#define fi first
#define sc second
#define re reserve
#define re1 v.resize
#define re2 a.resize
#define in insert
#define lp(i,k,n) for(ll i=0;i<n;i++)
using namespace std;
//continue
// @author: saleh_zizo
//std::reverse(b.begin(), b.end());
const ll N = 1e6;
void fast() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}
vector<ll>primes;
vector<bool> sieve(ll n){
 	vector<bool>isPrime(n+1,1);
 	isPrime[0]=isPrime[1]=0;
 	for(ll i=2; i<=n; i++){
   		if(isPrime[i]){
            primes.pb(i);
            for(ll j=2*i; j<=n; j+=i){
                isPrime[j]=0;
            }
   		}
 	}
 	return isPrime;
}
pair<ll, ll>pi(ll h , ll m , ll cnt) {
	while (cnt--) {
		m++;
		if (m == 60) {h++; m = 0;}
		if (h == 24) {h = 0;}
	}
	return {h, m};
}
ll checked_true(ll n){
    ll c = (n*(n+1))/2;
    return c;
}
vector<ll>primef(ll n){
    vector<ll>v;
    for(ll i=2;i*i<=n;i++){
       // vector<ll>temp;
        while(n%i==0){
            v.pb(i);
            n/=i;
        }
    }
    if(n>1){
        v.pb(n);
    }
    return v;
}
int main() { // 0 1 2 [0 , 1 , 2 , '\0'];
//freopen("window.in","r",stdin);
//cout<<fixed<<setprecision(3);// 1 2 3 4 5
	fast();
	sieve(N);
	auto v = primes;
	ll n , m; cin>>n>>m;
	ll arr[n][m];
	ll cnt = 2e9;
	lp(i,0,n){
	    ll temp = 0;
	    lp(j,0,m){
	        cin>>arr[i][j];
	        ll c = arr[i][j];
	        ll up= lower_bound (v.begin(), v.end(), c)-v.begin();
	        temp+=v[up]-c;
	    }
	    cnt = min(cnt,temp);
	}
	lp(i,0,m){
	    ll temp = 0;
	    lp(j,0,n){
	        ll c = arr[j][i];
	        ll up= lower_bound (v.begin(), v.end(), c)-v.begin();
	        temp+=v[up]-c;
	    }
	    cnt = min(cnt,temp);
	}
	cout<<cnt<<el;
}


Comments

Submit
0 Comments
More Questions

349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles